﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System.Data
{
    /// <summary>
    /// XDataTable
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class XDataTable<T> : System.Data.DataTable where T : XDataRow
    {

        #region cctor

        /// <summary>
        /// 
        /// </summary>
        public XDataTable()
        {
            base.BeginInit();
            this.InitClass();
            base.EndInit();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="table"></param>
        public XDataTable(System.Data.DataTable table)
        {
            base.TableName = table.TableName;
            if ((table.CaseSensitive != table.DataSet.CaseSensitive))
            {
                base.CaseSensitive = table.CaseSensitive;
            }
            if ((table.Locale.ToString() != table.DataSet.Locale.ToString()))
            {
                base.Locale = table.Locale;
            }
            if ((table.Namespace != table.DataSet.Namespace))
            {
                base.Namespace = table.Namespace;
            }
            base.Prefix = table.Prefix;
            base.MinimumCapacity = table.MinimumCapacity;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="info"></param>
        /// <param name="context"></param>
        protected XDataTable(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) :
            base(info, context)
        {
            this.InitVars();
        }

        #endregion

        /// <summary>
        /// InitVars
        /// </summary>
        protected virtual void InitVars()
        {

        }

        /// <summary>
        /// <para>InitClass</para>
        /// <para>初始化列</para>
        /// </summary>
        protected virtual void InitClass()
        {

        }

        #region DataTable

        /// <summary>
        /// 获取行的数量
        /// </summary>
        public int RowCount
        {
            get { return base.Rows.Count; }
        }

        /// <summary>
        /// 获取列的数量
        /// </summary>
        public int ColumnCount
        {
            get { return base.Columns.Count; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public T this[int index]
        {
            get { return (T)base.Rows[index]; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected override System.Data.DataTable CreateInstance()
        {
            return new XDataTable<T>();
        }

        /// <summary>
        /// 克隆 XDataTable<T> 的结构，包括所有 XDataTable<T> 架构和约束。
        /// </summary>
        /// <returns></returns>
        public override System.Data.DataTable Clone()
        {
            XDataTable<T> cln = (XDataTable<T>)base.Clone();
            cln.InitVars();
            return base.Clone();
        }

        #endregion

        #region Row

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public new T NewRow()
        {
            return (T)base.NewRow();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="row"></param>
        public void Add(T row)
        {
            base.Rows.Remove(row);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="row"></param>
        public void Remove(T row)
        {
            base.Rows.Remove(row);
        }

        /// <summary>
        /// TODO:NewRowFromBuilder
        /// </summary>
        /// <param name="builder"></param>
        /// <returns></returns>
        protected override System.Data.DataRow NewRowFromBuilder(System.Data.DataRowBuilder builder)
        {
            return new XDataRow(builder);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected override Type GetRowType()
        {
            return typeof(T);
        }

        #endregion
    }
}
